package com.xt.purchasingsystem.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xt.purchasingsystem.dto.SupplierApprovalDto;
import com.xt.purchasingsystem.mapper.SupplierApprovalDetailsMapper;
import com.xt.purchasingsystem.mapper.SupplierApprovalMapper;
import com.xt.purchasingsystem.model.SupplierApproval;
import com.xt.purchasingsystem.model.SupplierApprovalDetails;
import com.xt.purchasingsystem.service.SupplierApprovalService;
import com.xt.purchasingsystem.utils.IdWorker;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yhp
 * @since 2020-05-31
 */
@Service
@Transactional
public class SupplierApprovalServiceImpl extends ServiceImpl<SupplierApprovalMapper, SupplierApproval> implements SupplierApprovalService {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private SupplierApprovalMapper supplierApprovalMapper;
    @Autowired
    private SupplierApprovalDetailsMapper supplierApprovalDetailsMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result saveSupplierApproval(SupplierApprovalDto supplierApprovalDto) {

        String id=String.valueOf(idWorker.nextId());
        supplierApprovalDto.getSupplierApproval().setId(id);
        supplierApprovalMapper.insert(supplierApprovalDto.getSupplierApproval());
        for (SupplierApprovalDetails supplierApprovalDetail : supplierApprovalDto.getSupplierApprovalDetails()) {
            supplierApprovalDetail.setApprovalId(id);
            String did=String.valueOf(idWorker.nextId());
            supplierApprovalDetail.setId(did);
            supplierApprovalDetailsMapper.insert(supplierApprovalDetail);
        }
        return ResultFactory.buildSuccessResult("添加成功,需要审核");
    }

    @Override
    public Result getAllNoCheckAndNoApply(Integer currNo, Integer pageSize) {
        PageHelper.startPage(currNo,pageSize);
        List<SupplierApprovalDto> allNoCheckAndNoApply = supplierApprovalMapper.getAllNoCheckAndNoApply();
        PageInfo<SupplierApprovalDto> pageInfo = new PageInfo<>(allNoCheckAndNoApply);
        return ResultFactory.buildSuccessResult("查询成功",pageInfo);
    }

    @Override
    public Result checkSupplierApproval(SupplierApprovalDto supplierApprovalDto) {
        supplierApprovalMapper.updateById(supplierApprovalDto.getSupplierApproval());
        for (SupplierApprovalDetails supplierApprovalDetail : supplierApprovalDto.getSupplierApprovalDetails()) {
            supplierApprovalDetailsMapper.updateById(supplierApprovalDetail);
        }
        return ResultFactory.buildSuccessResult("审核成功");
    }
}
